package com.ailk.kbi.kbirazor.services

import groovy.util.logging.Slf4j

import org.springframework.stereotype.Service



import com.ailk.kbi.kbirazor.util.DBLocalUtil;
import com.ailk.kbi.kbirazor.util.DBSqlUtil;
import com.ailk.kbi.kbirazor.util.ExcelUtil;
import com.ailk.kbi.kbirazor.util.SettingUtil;
import com.ailk.kbi.kbirazor.vo.DimVO;
import com.ailk.kbi.kbirazor.vo.KpiVO;
import com.ailk.kbi.kbirazor.vo.MindMapVO
import com.ailk.kbi.kbirazor.vo.ResultVO;
import com.ailk.kbi.kbirazor.vo.SettingVO;
import com.ailk.kbi.kbirazor.vo.TimeRangeVO;
import com.ailk.kbi.kbirazor.vo.UserVO;

/**
 * 总的Flex服务
 * @author kyunra
 *
 */
@Slf4j
@Service
class FlexService {
	private SettingVO settingVO = SettingUtil.getSettingVO()
	
	String createExcel(excelXmlString){
		ExcelUtil excelUtil = new ExcelUtil();
		return excelUtil.createExcel(excelXmlString);
	}

	/**
	 * 加载脑图列表
	 * @return
	 */
	List<MindMapVO> getMindMapList(){
		log.info("加载脑图列表 ...")
		List<MindMapVO> mindMapList = []
		mindMapList.add(new MindMapVO(fileName:"income.xml",title:"收入变化分析"))
		mindMapList.add(new MindMapVO(fileName:"users_on_communication.xml",title:"月通讯用户变化分析"))
		mindMapList.add(new MindMapVO(fileName:"traffic.xml",title:"话务量变化分析"))
		mindMapList
	}

	/**
	 * 加载表单XML内容
	 * @param mindMapVO
	 * @return
	 */
	String loadMindMapXML(MindMapVO mindMapVO){
		if(mindMapVO == null){
			throw new Exception("脑图索引信息为空！")
		}
		log.info "加载脑图XML内容， xmlName = [" + mindMapVO.fileName + "] ..."
		String content = (new File(settingVO.getFilesRoot() + mindMapVO.fileName)).getText();
		content;
	}

	/**
	 * 获取时间范围
	 * 
	 * @param sql
	 * @return
	 */
	TimeRangeVO getTimeRangeVO(String sql) {
		log.info "获取时间范围， sql = [" + sql + "] ..."
//		DBLocalUtil dbUtil = new DBLocalUtil()
		DBSqlUtil dbUtil = new DBSqlUtil()
		TimeRangeVO timeRangeVO = dbUtil.getTimeRangeVO()
		log.info "获取时间范围， timeRangeVO = [" + timeRangeVO + "]。"
		timeRangeVO
	}

	List<KpiVO> getKpiValues(String currentDate, UserVO userVO, List<String> kpiIds){
		log.info "获取KPI的值， kpiIds = [" + kpiIds + "] ..."
//		DBLocalUtil dbUtil = new DBLocalUtil()
		DBSqlUtil dbUtil = new DBSqlUtil()
		List<KpiVO> values = dbUtil.getKpiValues(currentDate,userVO,kpiIds)
		values
	}
	 
	ResultVO getThresholdSetXML(List<String> kpiIds, UserVO userVO) {
		DBSqlUtil dbLocalUtil = new DBSqlUtil()
		ResultVO result = dbLocalUtil.getThresholdSetXML(kpiIds,userVO)
		result
	}
	
	ResultVO getDescDataXML(String condition) {
		DBSqlUtil dbLocalUtil = new DBSqlUtil()
		ResultVO result = dbLocalUtil.getDescDataXML(condition)
		result
	}
	
	ResultVO cityList(DimVO dimVO, List<String> kpiIdList) {
		log.info "获取下钻的各分公司的值， kpiIds =  ${kpiIdList}"
		
		DBSqlUtil dbLocalUtil = new DBSqlUtil()
		ResultVO result = dbLocalUtil.cityList(dimVO, kpiIdList)
		result
	}
	
	ResultVO areaList(DimVO dimVO, List<String> kpiIdList) {
		log.info "获取下钻的各分公司的值， kpiIds =  ${kpiIdList}"
		
		DBSqlUtil dbLocalUtil = new DBSqlUtil()
		ResultVO result = dbLocalUtil.areaList(dimVO, kpiIdList)
		result
	}
	
	ResultVO getTimeDataList(String opTime, List<String> kpiIdList) {
		log.info "获取时域分析数据， kpiIds =  ${kpiIdList}"
		DBSqlUtil dbLocalUtil = new DBSqlUtil()
		ResultVO result = dbLocalUtil.getTimeDataList(opTime, kpiIdList)
		result
	}
	
	ResultVO getBrandDataList(String opTime, List<String> kpiIdList) {
		log.info "获取时域分析数据， kpiIds =  ${kpiIdList}"
		DBSqlUtil dbLocalUtil = new DBSqlUtil()
		ResultVO result = dbLocalUtil.getBrandDataList(opTime, kpiIdList)
		result
	}
	
	int saveThresholdValue(String xmlStr,UserVO userVO) {
		log.info "保存阈值"
		DBSqlUtil dbLocalUtil = new DBSqlUtil()
		int result = dbLocalUtil.saveThresholdValue(xmlStr,userVO)
		result
	}
	
	List<String> getWarnKpiIds(String time, UserVO userVO, List<String> kpiIds){
		log.info "获取报警的KPI值， kpiIds = [" + kpiIds + "] ..."
		DBSqlUtil dbUtil = new DBSqlUtil()
		List<KpiVO> values = dbUtil.getWarnKpiIds(time,userVO,kpiIds)
		values
	}
	
}
